//
//  ByUISceneSaveView.m
//  laserdevice_ios
//
//  Created by pro on 16/12/15.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByUISceneSaveView.h"

@interface ByUISceneSaveView ()

@property (nonatomic, strong) UIView *bgView;

@end

@implementation ByUISceneSaveView
{
    NSArray *_functions;
    
    NSInteger _curIndex;
}
- (instancetype)initWithFunctions:(NSArray *)functions {
    
    if (self = [super init]) {
        
        _functions = functions;
        
        self.positionMode = ZKBombPositionModeCenter;
        self.popMode = ZKBombPopModeBottom;
        
        [self setupViews];
    }
    return self;
}


- (UIView *)bgView {
    
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.4, SCREEN_WIDTH * 0.7)];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.shadowOffset = CGSizeMake(0, 3);
        _bgView.layer.shadowOpacity = 0.5;
        _bgView.layer.shadowRadius = 3;
        _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
        _bgView.layer.cornerRadius = 3;
        self.contentView = _bgView;
    }
    return _bgView;
}


- (void)setupViews {
    

    
    CGFloat x, y, width, height;
    x = 0;
    y = 0;
    width = self.bgView.width;
    height = 40;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.font = FONT(14);
    titleLabel.numberOfLines = 2;
    titleLabel.text = STRING(@"xuanzebaocungeshi");
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:titleLabel];
    
    
    NSArray *texts = _functions;
    width = self.bgView.width;
    height = 15;
    CGFloat margin = 10;
    for (int i = 0; i < texts.count; i ++) {
        
        x = 0;
        y = titleLabel.bottom + i * (height + margin);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, width, height);
        button.titleLabel.font = FONT(14);
        [button setTitle:texts[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"scene_saveUnselected"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"scene_saveSelected"] forState:UIControlStateSelected];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 2, 0, -2)];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, -2, 0, 2)];
        [button addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:button];
        button.tag = i + 10;
        
        if (i == 0) {
            button.selected = YES;
            _curIndex = 0;
        }
    }
    
    x = 5;
    y = self.bgView.subviews.lastObject.bottom + 10;
    width = self.bgView.width - x * 2;
    height = 0.5;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = ZJ_SEPLINE_COLOR;
    [self.bgView addSubview:line];
    
    // 按钮
    // 确定和取消
    x = 10;
    y = line.bottom + 10;
    width = (self.bgView.width - 10 * 2 - 20) / 2;
    height = 25;
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(x, y, width, height);
    cancleBtn.titleLabel.font = FONT(15);
    cancleBtn.backgroundColor = [UIColor lightGrayColor];
    [cancleBtn setTitle:STRING(@"quxiao") forState:UIControlStateNormal];
    [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:cancleBtn];
    
    x = cancleBtn.right + 20;
    y = cancleBtn.top;
    width = cancleBtn.width;
    height = cancleBtn.height;
    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    okBtn.frame = CGRectMake(x, y, width, height);
    okBtn.backgroundColor = CJ_THEME_COLOR;
    okBtn.titleLabel.font = FONT(15);
    [okBtn setTitle:STRING(@"queding") forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [okBtn addTarget:self action:@selector(ok) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:okBtn];
    
    
    self.bgView.height = okBtn.bottom + 10;
    

    
}

- (void)tapAction:(UIButton *)sender {
    
    // 点击已经选中的，就不做操作
    if (sender.tag - 10 == _curIndex) {
        return;
    }
    
    UIButton *prevBtn = [self.bgView viewWithTag:_curIndex + 10];
    prevBtn.selected = NO;
    
    sender.selected = YES;
    
    _curIndex = sender.tag - 10;
}

- (void)cancle {
    
    [self remove];
}

- (void)ok {
    NSLog(@"%li", _curIndex);
    if (self.backAction) {
        self.backAction(_curIndex);
    }
    [self remove];
}

@end
